#!/usr/bin/python
import os
import sys

def init(project_name):
    DIRS = {}
    DIRS['root_dir'] = './%s' % (project_name)
    DIRS['build_dir'] = '%s/build' % (DIRS['root_dir'])
    DIRS['src_dir'] = '%s/src' % (DIRS['root_dir'])
    DIRS['main_dir'] = '%s/main' % (DIRS['src_dir'])
    DIRS['include_dir'] = '%s/include' % (DIRS['src_dir'])
    DIRS['common_dir'] = '%s/common' % (DIRS['src_dir'])
    return DIRS

def make_dir(DIRS, project_name):
    if os.path.exists(DIRS['root_dir']):
        print 'project: "%s" is exist' % (project_name)
        return False
    else:
        os.makedirs(DIRS['root_dir'])
        os.makedirs(DIRS['build_dir'])
        os.makedirs(DIRS['src_dir'])
        os.makedirs(DIRS['main_dir'])
        os.makedirs(DIRS['include_dir'])
        os.makedirs(DIRS['common_dir'])
        print 'step 1: create dir ok'
    return True

def make_file(DIRS, project_name, project_type):
    if not os.path.exists(DIRS['root_dir']):
        return False
    else:
        Makefile_path = '%s/Makefile' % (DIRS['build_dir'])
        write_file(Makefile_path, make_build_mk_string(project_name))

        Templatefile_path = '%s/template.mk' % (DIRS['root_dir'])
        write_file(Templatefile_path, template_mk_string())
        
        Main_C_path = '%s/main.%s' % (DIRS['main_dir'], project_type)
        write_file(Main_C_path, main_c_file_string())
        print 'step 2: make file ok'
    return True

def write_file(path, content):
    file = open(path, 'w')
    file.write(content)
    file.close()

def make_build_mk_string(project_name='', project_type='c'):
    return """
########################################################################
#			Compiler Flags
########################################################################
CFLAGS = -Wall -g -O0 
#-pg

CPPFLAGS = -Wall -g -O0
#-pg

########################################################################
#			Project Configure
########################################################################
ROOT_DIR = ..

VPATH = $(ROOT_DIR)/src/ : $(ROOT_DIR)/src/include : $(ROOT_DIR)/src/common : $(ROOT_DIR)/src/main

C_SRCS = main.%s

CPP_SRCS = 

INCS = -I$(ROOT_DIR)/src -I$(ROOT_DIR)/src/include -I$(ROOT_DIR)/src/common -I$(ROOT_DIR)/src/main

EXE = %s

LIBS = -lpthread

include $(ROOT_DIR)/template.mk""" % (project_type, project_name)

def template_mk_string():
    return """
########################################################################
#			Default Targets
########################################################################

.PHONY: all exe dirs clean 

all : exe dirs
exe : $(EXE)
dirs: $(DIRS)

########################################################################
#			Compilers
########################################################################

CC = gcc
CPP = g++

########################################################################
#			Executable Program Rules
########################################################################

$(EXE) : $(CPP_SRCS:.cpp=.o) $(C_SRCS:.c=.o)
\t$(CPP) $(CPPFLAGS) $(LIBS) -o $@ $^ 


########################################################################
#			Subdirectories
########################################################################

dirs: $(DIRS)

ifdef DIRS
.PHONY: $(DIRS)
$(DIRS):
\t@if [ -e $@/Makefile ] ; then $(MAKE) -C $@ $(MAKECMDGOALS) ; fi
endif

########################################################################
#			Implicit Rules
########################################################################

%.o: %.c
\t$(CC) $(CFLAGS) $(INCS) -c $<


%.o: %.cc
\t$(CPP) $(CPPFLAGS) $(INCS) -c $<


%.d: %.c
\t@set -e; rm -f $@; \
$(CC) -MM $(CFLAGS) $(INCS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$


%.d: %.cc
\t@set -e; rm -f $@; \
$(CPP) -MM $(CPPFLAGS) $(INCS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$


########################################################################
#			Cleanup Rules
########################################################################

clean: $(DIRS)
\trm -f *.o *.d *.d.* $(EXE)

########################################################################
#			Dependencies
########################################################################

-include $(CPP_SRCS:.cc=.d) $(C_SRCS:.c=.d)"""

def main_c_file_string():
    return"""
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
	printf("project test\\n");
	return 0;
}
"""

def print_usage():
    print '#############################################'
    print '### Thank you for using pjmaker'
    print '#############################################'
    print 'usage: pjmaker <project_name> <project_type>'
    print 'C project:\tpjmaker test c'
    print 'C++ project:\tpjmaker test cpp'

def main():
    if len(sys.argv) < 3:
        print_usage()
        return
    project_name = sys.argv[1]
    project_type = sys.argv[2]
    DIRS = init(project_name)
    if not make_dir(DIRS, project_name):
        return
    make_file(DIRS, project_name, project_type)

if __name__ == '__main__':
    main()
